package com.util;

import java.util.ArrayList;

import org.apache.log4j.Logger;


public class Validator {
	private static org.apache.log4j.Logger log = Logger.getLogger(Validator.class);
	public static XMLReader xmlreader;
public static void validateInput(String keyword,String contextString,String apiKey, String userID)throws Exception{
	try{
		xmlreader=new XMLReader();
	}catch(Exception e){
		throw new UnavailableWebServiceResourceException("Service configuration is not found");
	}
	if(keyword==null||keyword.equals("")){
		log.error("Invalid keyword");
		throw new InvalidInputException("Invalid keyword. Please enter a valid keyword");
	}
	if(contextString==null||contextString.equals("")){
		log.error("Invalid contextString");
		throw new InvalidInputException("Invalid contextString. Please enter a valid contextString");
	}
	if(apiKey==null||apiKey.equals("")){
		log.error("Invalid apiKey");
		throw new InvalidInputException("Invalid apiKey. Please enter a valid apiKey");
	}
	if(!xmlreader.isValidapiKey(apiKey)){
		log.error("Invalid apiKey");
		throw new InvalidInputException("Invalid apiKey. Please enter a valid apiKey");
	}
	String contextStringError=testContextString(contextString, apiKey, userID, xmlreader);
	if(!contextStringError.equals("ok")){
		throw new InvalidInputException(contextStringError);
	}
}


private static String testContextString(String contextString,String apiKey,String userID, XMLReader xReader){
	//boolean error=false;
	String serviceAlgorithm=xReader.getServiceAlgorithm(apiKey).trim();
	String errorString="Your context string \""+contextString+"\" seems  to be not valid. Please check it and send it again";
	if((serviceAlgorithm.equalsIgnoreCase("Simple Recommender"))||(serviceAlgorithm.equalsIgnoreCase("Userbased Recommender"))){
		ArrayList<String> contextVarsInFile=xReader.getContextVariables(apiKey);
		ArrayList<String> contextVarsReceived=new ArrayList<String>();
		String[] contextVarWithValues=contextString.split(",");
		for(int k=0;k<contextVarWithValues.length;k++){
			String tempContextVar=contextVarWithValues[k].split(":")[0].trim();
			if(tempContextVar!=null){
				contextVarsReceived.add(tempContextVar);
			}
		}
		if(contextVarsInFile.size()!=contextVarsReceived.size()){
			return errorString;
		}
		
		for(int k=0;k<contextVarsInFile.size();k++){
			boolean contextvariablesValid=false;
			for(int j=0;j<contextVarsReceived.size();j++){
				if((contextVarsInFile.get(k).toString()).equalsIgnoreCase((contextVarsReceived.get(j).toString()))){
					contextvariablesValid=true;
				}
			}
			if(!contextvariablesValid){
				return errorString;
			}
		}
		
		
	}
	
	else if((serviceAlgorithm.equalsIgnoreCase("tagbasedrecommenderwithusersimilarity"))||serviceAlgorithm.equalsIgnoreCase("tagbasedrecommender")){
		if(!("tags".equalsIgnoreCase(contextString.split(":")[0].trim()))){
			return errorString;
		}
	}
	
	
	if((serviceAlgorithm.equalsIgnoreCase("tagbasedrecommenderwithusersimilarity"))||(serviceAlgorithm.equalsIgnoreCase("Userbased Recommender"))){
		if(userID.equals("")||userID==null){
			return "Please enter a valid userID";
		}
		
	}
	
	return "ok";
}
}
